// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Linq.Expressions;

namespace SnapObjects.Expressions
{
    internal class EvaluateNestParseProvider : INestParseProvider<EvaluateResult>
    {
        public virtual string[] SupportKinds => new[] { SourceKind.Default };

        public virtual ParserOptions<Expression> CreateOptions(ParserOptions<EvaluateResult> options)
        {
            return new CompileParserOptions(
                null,
                SourceKind.Default,
                options.DataSource?.GetType(),
                options.DataSource,
                options.CustomData);
        }

        public virtual EvaluateResult Invoke(
            ParserOptions<EvaluateResult> options,
            Expression nestExpression,
            ParserOptions<Expression> nestOptions)
        {
            var script = new CompileScript(nestExpression, nestOptions);

            return new EvaluateResult(script.Compile().Invoke(options.DataSource));
        }
    }
}
